*===============================================================================
* Project title		Savings Accounts to Borrow Less	
* PIs			Felipe Kast, Dina Pomeranz 
* File name		6_tables_analysis_other_outcomes.do
*-------------------------------------------------------------------------------
* Description		Creates table on bulky expenditure and HH dynamics 
* Outputs		Table A19 
* Spaces per tab 	8 
*===============================================================================

*===============================================================================
*TOC
*1)	Prepare data for analysis
*2)	Tables
*===============================================================================

*===============================================================================
* Section 1 - Prepare data for analysis
*===============================================================================

clear all
global path "~/file_server/project_savings/2_shared/impact/do/November2019"	// Path to processed data
global results "$path/results"		
cd "$results"									// Change to directory to store results
set more off

*Loading processed data
	use $path/datasets/impactDatabase.dta, clear

*===============================================================================
* Section 2 - Tables 
*===============================================================================
* 2.1.Calculates table content -------------------------------------------------
*-------------------------------------------------------------------------------

* Household dynamics 

foreach y in savingDecision hideSavingsFamily hideSavingsSO ///			// Variables: who generally makes the decision to save, do you keep savings hidden from your family (children, other relatives, not 
 borrowSO troubleSO {								// partner), is your partner aware of all your savings, in the last 3 months have you had to ask for extra money from your partner,
										// in the last 3 months have you had trouble with your partner for money reasons
// mark subjects nonmissing in both period	
	reg d.`y' 
	egen t1 = max(e(sample)), by(idBaseSurvey)
	
// Main regression 	
	
	reghdfe `y' c.accountAccess#c.post post  if t1 == 1, ///		// Regressing borrowing variables on treatment x post if non-missing in both periods  
	abs(idBaseSurvey) vce(cluster groupId) 					// Individual FE, Stratum FE x Post, SE clustered at group level
	
//marking observations used in the regression 	
	gen `y'_s = e(sample) 
		
//adding control mean		
	sum `y' if accountAccess == 0 & post == 1
	if (abs(`r(mean)') < 100) {
		estadd local control_mean = trim("`: display %12.3fc r(mean)'")
	} 
	else {
		estadd local control_mean = trim("`: display %12.0fc r(mean)'")
	}
		
	drop t1
	distinct idBaseSurvey  if e(sample)					// Adding FE and N to estimates stored 
	estadd scalar num_ind        = r(ndistinct)
 	estadd local ind_fixeff      = "Yes"
	eststo A`i'
	local i = `i' + 1 //updates counter	
	
}

* Bulky expenditures 

foreach y in boughtElectronics madeInvestment improveHome {			// Variables: did you buy a TV/radio/computer, make an investment in equipment/business, make any home improvements
 								

// mark subjects nonmissing in both period	
	reg d.`y' 
	egen t1 = max(e(sample)), by(idBaseSurvey)
	
// Main regression 	
	
	reghdfe `y' c.accountAccess#c.post post  if t1 == 1, ///		// Regressing borrowing variables on treatment x post if non-missing in both periods  
	abs(idBaseSurvey) vce(cluster groupId) 					// Individual FE, Stratum FE x Post, SE clustered at group level
	
//marking observations used in the regression 	
	gen `y'_s = e(sample) 
		
//adding control mean		
	sum `y' if accountAccess == 0 & post == 1
	if (abs(`r(mean)') < 100) {
		estadd local control_mean = trim("`: display %12.3fc r(mean)'")
	} 
	else {
		estadd local control_mean = trim("`: display %12.0fc r(mean)'")
	}
		
	drop t1
	distinct idBaseSurvey  if e(sample)					// Adding FE and N to estimates stored 
	estadd scalar num_ind        = r(ndistinct)
 	estadd local ind_fixeff      = "Yes"
	eststo B`i'
	local i = `i' + 1 //updates counter	
}

*-------------------------------------------------------------------------------
* 2.2 Writes table -------------------------------------------------------------
*-------------------------------------------------------------------------------

	esttab A* B* using other_variables_impact.tex, replace 									///
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber 						///
	stats( control_mean num_ind N ind_fixeff, fmt(%12.0fc %12.0fc %12.0fc) 							///
	label("Control mean" "\hline Individuals" "Observations" "Individual FE" )) mlabels(,none)				///
	collabels(, none) eqlabels(, none) 											/// 
	keep(c.accountAccess#c.post )  												///
	varlabels(c.accountAccess#c.post "Account $\times$ post") 								///
	starlevels(* 0.1 ** 0.05 *** 0.01) 											///
	prehead(\begin{tabular}{p{3 cm} c*{@M}{c}} \hline \hline 								///
	& \multicolumn{5}{c}{Family Dynamics} & \multicolumn{3}{c}{Bulky Expenditures}   					///
	\\ \cmidrule(lr){2-6} \cmidrule(lr){7-9} 										///
	& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ 									///
	& Decisions 	& Savings  	& Savings  	& Borrowed  	& Conflicts  	& Electronics 	& Business  		& Home  	\\ 	///
	& about  	& hidden from  	& hidden from  	& from 		& with 		&  		& investment/ 		& improvements  \\  	///
	& saving 	& family 	& partner 	& partner 	& partner 	&  		& equipment 		&   		\\ ) 	///
	posthead(\hline) prefoot( ) 												///
	postfoot(\hline \hline \end{tabular}) 
	
	
